<?php

include_once("dbconnect.php");

if (isset($_POST['id'])) {
    $id = ($_POST['id']);
    $query = "DELETE FROM `manufacturers` WHERE id='$id'";
    mysqli_query(getConnect(), $query);
    echo $id;
}

function getManuID($name) {
    //$query_Manu = "select * from `manufacturers` ";
    $id = "";
    $result = mysqli_query(getConnect(), "Select * from `manufacturers` where name ='$name'");
    while ($row = mysqli_fetch_array($result)) {
        $id = $row['id'];
    }
    return $id;
}

if (isset($_POST['addnew'])) {
    $name = ($_POST['addnew']);
    $cateId = ($_POST['category']);
    $listCate = split(';', $cateId);
    // echo count($listCate);

    $query_Manu = "INSERT INTO  `manufacturers`(name) VALUES ('$name')";
    if (!mysqli_query(getConnect(), $query_Manu)) {
        die('Error: ');
    }
    $manuId = getManuID($name);
    for ($i = 0; $i < (count($listCate) - 1); $i++) {
        $query_Cate_Manu = "INSERT INTO  `cate_manu` VALUES ($listCate[$i],$manuId)";
        echo $query_Cate_Manu;
        if (!mysqli_query(getConnect(), $query_Cate_Manu)) {
            die('Error manu cate: ');
        }
    }



    echo "OK";
}
if (isset($_POST['new'])) {
    $re = "00";
    $newManu = trim(strtoupper($_POST['new']));
    $result = mysqli_query(getConnect(), "Select * from `manufacturers`");
    while ($row = mysqli_fetch_array($result)) {

        if ($newManu == strtoupper($row['name'])) {
            $re = "1";
        }
    }
    echo $re;
}

if (isset($_POST['newName'])) {
    $re = "00";
    $newId = $_POST['newName'];
    $oldName = $_POST['oldName'];
    $query = "UPDATE  `manufacturers` SET name = '$newId' where name ='$oldName' ";
    if (!mysqli_query(getConnect(), $query)) {
        die('Error: ');
    }
    echo "OK";
}

function getCheckboxCates() {
    $str = "<div class='checkbox' style='padding-left:150px'>";
    $result = mysqli_query(getConnect(), "Select * from `categories`");
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . "<label class='col-md-4 'style='margin-top:20px'><input type='checkbox' value='${row['id']}' >${row['name']}</label>";
    }
    $str = $str . "</div>";
    echo $str;
}

function listCate($manu) {
    $str = "";
    $result = mysqli_query(getConnect(), "select categories.name from categories inner join cate_manu on cate_manu.category = categories.id where cate_manu.manufacturer = $manu");
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . "<option>{$row['name']}</option>";
    }
    return $str;
}

function displayBody($role) {
    $str = "<tbody>";
    $result = mysqli_query(getConnect(), "Select * from `manufacturers`");
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . " <tr class='odd gradeX'>
                       <td style='width:50%'>${row['name']}</td>";
        $cateArr = listCate($row['id']);
        if ($role == 1) {
            $str = $str . " <td style='width:20%;text-align:center;vertical-align:middle;'><select class='form-control' style='width:90%;'>$cateArr</select></td>
                            <td class='center' style='text-align:center;vertical-align:middle;width:15%'>
                          
                            <i class='fa fa-wrench fa-fw'></i>
                            <button   type='button' data-target='#myModal' data-toggle='modal' class='btn btn-link' value ='edit'>Chỉnh sửa</button></td>
                       <td class='center' style='text-align:center;vertical-align:middle;width:15%' >
                            <i class='fa fa-bitbucket'></i>
                            <button  id ='${row['id']}' type='button' class='btn btn-link' value ='del'>Xóa</button></td>
                   </tr>";
        } elseif ($role == 2) {
            $str = $str . " <td style='width:20%;text-align:center;vertical-align:middle;'><select class='form-control' style='width:90%;'>$cateArr</select></td>
                            <td class='center' style='text-align:center;vertical-align:middle;width:15%'>
                            <i class='fa fa-wrench fa-fw'></i>
                            <button  disabled type='button' data-target='#myModal' data-toggle='modal' class='btn btn-link' value ='edit'>Chỉnh sửa</button></td>
                       <td class='center' style='text-align:center;vertical-align:middle;width:15%' >
                            <i class='fa fa-bitbucket'></i>
                            <button disabled id ='${row['id']}' type='button' class='btn btn-link' value ='del'>Xóa</button></td>
                   </tr>";
        }
    }
    $str = $str . " </tbody>";
    echo $str;
}

?>
